﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waker.Entitys;
using zkemkeeper;
using Waker.Common;

namespace Waker.APIs.ZKT
{
    public static class ZKTHelper
    {
        private static zkemkeeper.CZKEMClass axCZKEM1 = new zkemkeeper.CZKEMClass();
        //private static bool Session.Finger_IsConneted = false;//the boolean value identifies whether the device is connected
        private static int iMachineNumber = 1;//the serial number of the device.After connecting the device ,this value will be changed.

        public static En_Result ZKTConnect()
        {
            try
            {
                if (string.IsNullOrEmpty(Session.Finger_IP))
                    return En_Result.CreateFailedResult("请先设置指纹机的IP地址");
                if (Session.Finger_Port < 1)
                    return En_Result.CreateFailedResult("请先设置指纹机的端口");
                string ip = Session.Finger_IP;
                int port = Session.Finger_Port;
                int idwErrorCode = 0;
                Session.Finger_IsConneted = axCZKEM1.Connect_Net(ip, port);
                if (Session.Finger_IsConneted == true)
                {
                    //btnConnect.Text = "DisConnect";
                    //btnConnect.Refresh();
                    //lblState.Text = "Current State:Connected";
                    iMachineNumber = 1;//In fact,when you are using the tcp/ip communication,this parameter will be ignored,that is any integer will all right.Here we use 1.
                    axCZKEM1.RegEvent(iMachineNumber, 65535);//Here you can register the realtime events that you want to be triggered(the parameters 65535 means registering all)
                    //ZKTPowerOffDevice();
                    return En_Result.CreateSuccessResult();
                }
                else
                {
                    axCZKEM1.GetLastError(ref idwErrorCode);
                    //MessageBox.Show("Unable to connect the device,ErrorCode=" + idwErrorCode.ToString(), "Error");
                    return En_Result.CreateFailedResult("操作失败,错误代码:" + idwErrorCode.ToString());
                }
            }
            catch (Exception ex)
            {
                return En_Result.CreateFailedResult(ex.Message);
            }
        }
        public static En_Result ZKTConnectTest(string ip, int port)
        {
            try
            {
                int idwErrorCode = 0;
                Session.Finger_IsConneted = axCZKEM1.Connect_Net(ip, port);
                if (Session.Finger_IsConneted == true)
                {
                    //btnConnect.Text = "DisConnect";
                    //btnConnect.Refresh();
                    //lblState.Text = "Current State:Connected";
                    iMachineNumber = 1;//In fact,when you are using the tcp/ip communication,this parameter will be ignored,that is any integer will all right.Here we use 1.
                    axCZKEM1.RegEvent(iMachineNumber, 65535);//Here you can register the realtime events that you want to be triggered(the parameters 65535 means registering all)
                    //ZKTPowerOffDevice();
                    return En_Result.CreateSuccessResult();
                }
                else
                {
                    axCZKEM1.GetLastError(ref idwErrorCode);
                    //MessageBox.Show("Unable to connect the device,ErrorCode=" + idwErrorCode.ToString(), "Error");
                    return En_Result.CreateFailedResult("操作失败,错误代码:" + idwErrorCode.ToString());
                }
            }
            catch (Exception ex)
            {
                return En_Result.CreateFailedResult(ex.Message);
            }
        }
        public static En_Result ZKTSetDeviceTime()
        {
            try
            {
                if (Session.Finger_IsConneted == false)
                {
                    var res = ZKTConnect();
                    if (!res.IsSuccess)
                    {
                        return res;
                    }
                }
                int idwErrorCode = 0;
                axCZKEM1.EnableDevice(iMachineNumber, false);
                if (axCZKEM1.SetDeviceTime(iMachineNumber))
                {
                    ZKTRefreshData();
                    axCZKEM1.EnableDevice(iMachineNumber, true);
                    return En_Result.CreateSuccessResult();
                }
                else
                {
                    axCZKEM1.GetLastError(ref idwErrorCode);
                    axCZKEM1.EnableDevice(iMachineNumber, true);
                    //MessageBox.Show("Operation failed,ErrorCode=" + idwErrorCode.ToString(), "Error");
                    return En_Result.CreateFailedResult("同步时间失败,错误代码:" + idwErrorCode.ToString());
                }
            }
            catch (Exception ex)
            {
                return En_Result.CreateFailedResult(ex.Message);
            }
        }
        public static bool ZKTDisConnect()
        {
            try
            {
                //ZKTPowerOffDevice();
                axCZKEM1.Disconnect();
                Session.Finger_IsConneted = false;
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool ZKTRefreshData()
        {
            //try
            //{
            //    return axCZKEM1.RefreshData(iMachineNumber);
            //}
            //catch
            //{
            //    return false;
            //}
            return true;
        }
        public static bool ZKTCancelOperation()
        {
            try
            {
                return axCZKEM1.CancelOperation();
            }
            catch
            {
                return false;
            }
        }
        public static En_Result<string> ZKTGetDeviceIP()
        {
            try
            {
                if (Session.Finger_IsConneted == false)
                {
                    var res = ZKTConnect();
                    if (!res.IsSuccess)
                    {
                        return En_Result<string>.CreateFailedResult(res.Message);
                    }
                }
                int idwErrorCode = 0;

                string sIP = "";


                if (axCZKEM1.GetDeviceIP(iMachineNumber, ref sIP))
                {
                    return En_Result<string>.CreateSuccessResult(sIP);
                }
                else
                {
                    axCZKEM1.GetLastError(ref idwErrorCode);
                    //MessageBox.Show("Operation failed,ErrorCode=" + idwErrorCode.ToString(), "Error");
                    return En_Result<string>.CreateFailedResult("获取ip失败,错误代码:" + idwErrorCode.ToString());
                }
            }
            catch (Exception ex)
            {
                return En_Result<string>.CreateFailedResult(ex.Message);
            }
        }
        public static En_Result ZKTSetDeviceIP(string ip)
        {
            try
            {
                if (Session.Finger_IsConneted == false)
                {
                    var res = ZKTConnect();
                    if (!res.IsSuccess)
                    {
                        return En_Result.CreateFailedResult(res.Message);
                    }
                }
                int idwErrorCode = 0;
                if (axCZKEM1.SetDeviceIP(iMachineNumber, ip))
                {
                    ZKTRefreshData();
                    return En_Result.CreateSuccessResult();
                }
                else
                {
                    axCZKEM1.GetLastError(ref idwErrorCode);
                    //MessageBox.Show("Operation failed,ErrorCode=" + idwErrorCode.ToString(), "Error");
                    return En_Result.CreateFailedResult("设置ip失败,错误代码:" + idwErrorCode.ToString());
                }
            }
            catch (Exception ex)
            {
                return En_Result.CreateFailedResult(ex.Message);
            }
        }
        public static En_Result<string> ZKTGetDeviceMAC()
        {
            try
            {
                if (Session.Finger_IsConneted == false)
                {
                    var res = ZKTConnect();
                    if (!res.IsSuccess)
                    {
                        return En_Result<string>.CreateFailedResult(res.Message);
                    }
                }
                int idwErrorCode = 0;

                string sMAC = "";
                if (axCZKEM1.GetDeviceMAC(iMachineNumber, ref sMAC))
                {
                    return En_Result<string>.CreateSuccessResult(sMAC);
                }
                else
                {
                    axCZKEM1.GetLastError(ref idwErrorCode);
                    //MessageBox.Show("Operation failed,ErrorCode=" + idwErrorCode.ToString(), "Error");
                    return En_Result<string>.CreateFailedResult("获取MAC地址失败,错误代码:" + idwErrorCode.ToString());
                }
            }
            catch (Exception ex)
            {
                return En_Result<string>.CreateFailedResult(ex.Message);
            }
        }
        public static En_Result<DateTime> ZKTGetDeviceTime()
        {
            try
            {
                if (Session.Finger_IsConneted == false)
                {
                    var res = ZKTConnect();
                    if (!res.IsSuccess)
                    {
                        return En_Result<DateTime>.CreateFailedResult(res.Message);
                    }
                }
                int idwErrorCode = 0;

                int idwYear = 0;
                int idwMonth = 0;
                int idwDay = 0;
                int idwHour = 0;
                int idwMinute = 0;
                int idwSecond = 0;
                if (axCZKEM1.GetDeviceTime(iMachineNumber, ref idwYear, ref idwMonth, ref idwDay, ref idwHour, ref idwMinute, ref idwSecond))
                {
                    return En_Result<DateTime>.CreateSuccessResult(new DateTime(idwYear, idwMonth, idwDay, idwHour, idwMinute, idwSecond));
                }
                else
                {
                    axCZKEM1.GetLastError(ref idwErrorCode);
                    //MessageBox.Show("Operation failed,ErrorCode=" + idwErrorCode.ToString(), "Error");
                    return En_Result<DateTime>.CreateFailedResult("获取时间失败,错误代码:" + idwErrorCode.ToString());
                }
            }
            catch (Exception ex)
            {
                return En_Result<DateTime>.CreateFailedResult(ex.Message);
            }
        }
        /// <summary>
        ///1管理员数量2注册用户数量3机器内指纹模板数量4密码数量5操作记录数6考勤记录数7指纹模板容量8用户容量9 考勤记录容量10剩余指纹模板容量11剩余用户数容量12剩余考勤记录容量 其他状况返回0
        /// </summary>
        /// <param name="type">需获取的数据，范围为1-22，含义如下 1管理员数量 2注册用户数量 3机器内指纹模板数量 4密码数量 5操作记录数 6考勤记录数 7指纹模板容量 8用户容量 9考勤记录容量 10剩余指纹模板容量 11剩余用户数容量 12剩余考勤记录容量 其他状况返回0</param>
        /// <returns></returns>
        public static En_Result<int> ZKTGetDeviceStatus(int type)
        {
            try
            {
                if (type < 1 || type > 12)
                    return En_Result<int>.CreateFailedResult("数据类型必须在1和12之间");
                if (Session.Finger_IsConneted == false)
                {
                    var res = ZKTConnect();
                    if (!res.IsSuccess)
                    {
                        return En_Result<int>.CreateFailedResult(res.Message);
                    }
                }
                int idwErrorCode = 0;

                int value = 0;


                if (axCZKEM1.GetDeviceStatus(iMachineNumber, type, ref value))
                {
                    return En_Result<int>.CreateSuccessResult(value);
                }
                else
                {
                    axCZKEM1.GetLastError(ref idwErrorCode);
                    //MessageBox.Show("Operation failed,ErrorCode=" + idwErrorCode.ToString(), "Error");
                    return En_Result<int>.CreateFailedResult("获取状态数据失败,错误代码:" + idwErrorCode.ToString());
                }
            }
            catch (Exception ex)
            {
                return En_Result<int>.CreateFailedResult(ex.Message);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="type">该参数指定需清除的记录类型，范围为1—5，具体含义如下1考勤记录 2指纹模板数据 3无 4操作记录 5用户信息 当该参数为5时，即删除机器内所有用户，注：此操作同时会删除机器内所有指纹模板</param>
        /// <returns></returns>
        public static En_Result ZKTClearData(int type)
        {
            try
            {
                if (Session.Finger_IsConneted == false)
                {
                    var res = ZKTConnect();
                    if (!res.IsSuccess)
                    {
                        return En_Result.CreateFailedResult(res.Message);
                    }
                }
                int idwErrorCode = 0;
                axCZKEM1.EnableDevice(iMachineNumber, false);
                if (axCZKEM1.ClearData(iMachineNumber, type))
                {
                    ZKTRefreshData();
                    axCZKEM1.EnableDevice(iMachineNumber, true);
                    return En_Result.CreateSuccessResult();
                }
                else
                {
                    axCZKEM1.GetLastError(ref idwErrorCode);
                    axCZKEM1.EnableDevice(iMachineNumber, true);
                    //MessageBox.Show("Operation failed,ErrorCode=" + idwErrorCode.ToString(), "Error");
                    return En_Result.CreateFailedResult("清除数据失败,错误代码:" + idwErrorCode.ToString());
                }
            }
            catch (Exception ex)
            {
                return En_Result.CreateFailedResult(ex.Message);
            }
        }
        public static En_Result ZKTSetWorkCode(int id, int code)
        {
            try
            {
                if (id < 1 || code < 1)
                {
                    return En_Result.CreateFailedResult("id和编码不能为空");
                }
                if (Session.Finger_IsConneted == false)
                {
                    var res = ZKTConnect();
                    if (!res.IsSuccess)
                    {
                        return res;
                    }
                }


                int idwErrorCode = 0;
                axCZKEM1.EnableDevice(iMachineNumber, false);
                if (axCZKEM1.SetWorkCode(id, code))
                {
                    ZKTRefreshData();
                    axCZKEM1.EnableDevice(iMachineNumber, true);
                    return En_Result.CreateSuccessResult();
                }
                else
                {
                    axCZKEM1.GetLastError(ref idwErrorCode);
                    axCZKEM1.EnableDevice(iMachineNumber, true);
                    return En_Result.CreateFailedResult("操作失败,错误代码:" + idwErrorCode.ToString());
                }
            }
            catch (Exception ex)
            {
                return En_Result.CreateFailedResult(ex.Message);
            }
        }
        public static En_Result<string> ZKTGetWorkCode(int id)
        {
            try
            {
                if (id < 1)
                {
                    return En_Result<string>.CreateFailedResult("id不能为空");
                }
                if (Session.Finger_IsConneted == false)
                {
                    var res = ZKTConnect();
                    if (!res.IsSuccess)
                    {
                        return En_Result<string>.CreateFailedResult(res.Message);
                    }
                }


                int idwErrorCode = 0;
                int iAWorkCode = 0;
                if (axCZKEM1.GetWorkCode(id, out iAWorkCode))
                {
                    axCZKEM1.RefreshData(iMachineNumber);//the data in the device should be refreshed
                    return En_Result<string>.CreateSuccessResult(iAWorkCode.ToString());
                }
                else
                {
                    axCZKEM1.GetLastError(ref idwErrorCode);
                    return En_Result<string>.CreateFailedResult("操作失败,错误代码:" + idwErrorCode.ToString());
                }
            }
            catch (Exception ex)
            {
                return En_Result<string>.CreateFailedResult(ex.Message);
            }
        }
        public static En_Result ZKTRestartDevice()
        {
            try
            {
                if (Session.Finger_IsConneted == false)
                {
                    var res = ZKTConnect();
                    if (!res.IsSuccess)
                    {
                        return res;
                    }
                }
                int idwErrorCode = 0;
                if (axCZKEM1.RestartDevice(iMachineNumber) == true)
                {
                    //MessageBox.Show("The device will restart!", "Success");
                    return En_Result.CreateSuccessResult();
                }
                else
                {
                    axCZKEM1.GetLastError(ref idwErrorCode);
                    //MessageBox.Show("Operation failed,ErrorCode=" + idwErrorCode.ToString(), "Error");
                    return En_Result.CreateFailedResult("操作失败,错误代码:" + idwErrorCode.ToString());
                }
            }
            catch (Exception ex)
            {
                return En_Result.CreateFailedResult(ex.Message);
            }
        }
        /// <summary>
        /// 启用标志，1为启用，0为禁用
        /// </summary>
        /// <returns></returns>
        public static En_Result ZKTEnableDevice(bool flag)
        {
            try
            {
                if (Session.Finger_IsConneted == false)
                {
                    var res = ZKTConnect();
                    if (!res.IsSuccess)
                    {
                        return res;
                    }
                }
                int idwErrorCode = 0;
                if (axCZKEM1.EnableDevice(iMachineNumber, flag) == true)
                {
                    //MessageBox.Show("The device will restart!", "Success");
                    return En_Result.CreateSuccessResult();
                }
                else
                {
                    axCZKEM1.GetLastError(ref idwErrorCode);
                    //MessageBox.Show("Operation failed,ErrorCode=" + idwErrorCode.ToString(), "Error");
                    return En_Result.CreateFailedResult("操作失败,错误代码:" + idwErrorCode.ToString());
                }
            }
            catch (Exception ex)
            {
                return En_Result.CreateFailedResult(ex.Message);
            }
        }
        public static En_Result ZKTPowerOffDevice()
        {
            try
            {
                //if (Session.Finger_IsConneted == false)
                //{
                //    var res = ZKTConnect();
                //    if (!res.IsSuccess)
                //    {
                //        return res;
                //    }
                //}
                //int idwErrorCode = 0;
                //if (axCZKEM1.PowerOffDevice(iMachineNumber))
                //{
                //    //MessageBox.Show("The device will restart!", "Success");
                return En_Result.CreateSuccessResult();
                //}
                //else
                //{
                //    axCZKEM1.GetLastError(ref idwErrorCode);
                //    //MessageBox.Show("Operation failed,ErrorCode=" + idwErrorCode.ToString(), "Error");
                //    return En_Result.CreateFailedResult("操作失败,错误代码:" + idwErrorCode.ToString());

                //}
            }
            catch (Exception ex)
            {
                return En_Result.CreateFailedResult(ex.Message);
            }
        }

        public static En_Result ZKTStartEnroll(int id, int index, Action<Int32, Int32, Int32, Int32> OnEnrollFinger)
        {
            try
            {
                if (Session.Finger_IsConneted == false)
                {
                    var res = ZKTConnect();
                    if (!res.IsSuccess)
                    {
                        return res;
                    }
                }
                int idwErrorCode = 0;
                if (axCZKEM1.RegEvent(iMachineNumber, 65535))
                {
                    axCZKEM1.CancelOperation();
                    var a = axCZKEM1.DelUserTmp(iMachineNumber, id, index);
                    if (axCZKEM1.StartEnroll(id, index))
                    {
                        axCZKEM1.OnEnrollFinger -= new _IZKEMEvents_OnEnrollFingerEventHandler(OnEnrollFinger);
                        axCZKEM1.OnEnrollFinger += new _IZKEMEvents_OnEnrollFingerEventHandler(OnEnrollFinger);
                        axCZKEM1.StartIdentify();
                        return En_Result.CreateSuccessResult();
                    }
                }
                axCZKEM1.GetLastError(ref idwErrorCode);
                //MessageBox.Show("Operation failed,ErrorCode=" + idwErrorCode.ToString(), "Error");
                return En_Result.CreateFailedResult("操作失败,错误代码:" + idwErrorCode.ToString());

            }
            catch (Exception ex)
            {
                return En_Result.CreateFailedResult(ex.Message);
            }
        }

        public static En_Result ZKTStartVerify(int id, int index)
        {
            try
            {
                if (Session.Finger_IsConneted == false)
                {
                    var res = ZKTConnect();
                    if (!res.IsSuccess)
                    {
                        return res;
                    }
                }
                int idwErrorCode = 0;
                if (axCZKEM1.StartVerify(id, index))
                {
                    //MessageBox.Show("The device will restart!", "Success");
                    return En_Result.CreateSuccessResult();
                }
                else
                {
                    axCZKEM1.GetLastError(ref idwErrorCode);
                    //MessageBox.Show("Operation failed,ErrorCode=" + idwErrorCode.ToString(), "Error");
                    return En_Result.CreateFailedResult("操作失败,错误代码:" + idwErrorCode.ToString());
                }
            }
            catch (Exception ex)
            {
                return En_Result.CreateFailedResult(ex.Message);
            }
        }
        public static bool ZKTStartIdentify()
        {
            try
            {
                return axCZKEM1.StartIdentify();
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="name"></param>
        /// <param name="userId">用户号</param>
        /// <param name="pwd"></param>
        /// <param name="privilege">用户权限，0为普通用户，1为登记员，2为管理员，3为超级管理员</param>
        /// <param name="enable">用户启用标志，1为启用，0为禁用</param>
        /// <returns></returns>
        public static En_Result ZKTSetUserInfo(string name, int userId, string pwd, int privilege, bool enable)
        {
            try
            {
                if (Session.Finger_IsConneted == false)
                {
                    var res = ZKTConnect();
                    if (!res.IsSuccess)
                    {
                        return res;
                    }
                }
                int idwErrorCode = 0;
                axCZKEM1.EnableDevice(iMachineNumber, false);
                if (axCZKEM1.SetUserInfo(iMachineNumber, userId, name, pwd, privilege, enable))
                {
                    ZKTRefreshData();
                    axCZKEM1.EnableDevice(iMachineNumber, true);
                    //MessageBox.Show("The device will restart!", "Success");
                    return En_Result.CreateSuccessResult();
                }
                else
                {
                    axCZKEM1.GetLastError(ref idwErrorCode);
                    //MessageBox.Show("Operation failed,ErrorCode=" + idwErrorCode.ToString(), "Error");
                    return En_Result.CreateFailedResult("操作失败,错误代码:" + idwErrorCode.ToString());
                }
            }
            catch (Exception ex)
            {
                return En_Result.CreateFailedResult(ex.Message);
            }

        }
        /// <summary>
        /// 获取用户
        /// </summary>
        /// <param name="name"></param>
        /// <param name="userId">用户号</param>
        /// <param name="pwd"></param>
        /// <param name="privilege">用户权限，0为普通用户，1为登记员，2为管理员，3为超级管理员</param>
        /// <param name="enable">用户启用标志，1为启用，0为禁用</param>
        /// <returns></returns>
        public static En_Result ZKTGetUserInfo(int userId, ref string name, ref string pwd, ref int privilege, ref bool enable)
        {
            try
            {
                if (Session.Finger_IsConneted == false)
                {
                    var res = ZKTConnect();
                    if (!res.IsSuccess)
                    {
                        return res;
                    }
                }
                int idwErrorCode = 0;
                if (axCZKEM1.GetUserInfo(iMachineNumber, userId, ref name, ref pwd, ref privilege, ref enable))
                {
                    //MessageBox.Show("The device will restart!", "Success");
                    return En_Result.CreateSuccessResult();
                }
                else
                {
                    axCZKEM1.GetLastError(ref idwErrorCode);
                    //MessageBox.Show("Operation failed,ErrorCode=" + idwErrorCode.ToString(), "Error");
                    return En_Result.CreateFailedResult("操作失败,错误代码:" + idwErrorCode.ToString());
                }
            }
            catch (Exception ex)
            {
                return En_Result.CreateFailedResult(ex.Message);
            }

        }
        public static En_Result<string> ZKTGetUserTmpExStr(int id, int index, out int flag)
        {
            flag = -1;
            try
            {
                if (id < 1)
                {
                    return En_Result<string>.CreateFailedResult("id不能为空");
                }
                if (Session.Finger_IsConneted == false)
                {
                    var res = ZKTConnect();
                    if (!res.IsSuccess)
                    {
                        return En_Result<string>.CreateFailedResult(res.Message);
                    }
                }


                int idwErrorCode = 0;
                string tempData;
                int tempDataLenth;
                if (axCZKEM1.GetUserTmpExStr(iMachineNumber, id.ToString(), index, out flag, out tempData, out tempDataLenth))
                {
                    ZKTRefreshData();
                    return En_Result<string>.CreateSuccessResult(tempData);
                }
                else
                {
                    axCZKEM1.GetLastError(ref idwErrorCode);
                    return En_Result<string>.CreateFailedResult("操作失败,错误代码:" + idwErrorCode.ToString());
                }
            }
            catch (Exception ex)
            {
                return En_Result<string>.CreateFailedResult(ex.Message);
            }
        }
        /// <summary>
        /// 设置用户可用不可用
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="enable"></param>
        /// <returns></returns>
        public static En_Result ZKTEnableUser(int userId, bool enable)
        {
            try
            {
                if (Session.Finger_IsConneted == false)
                {
                    var res = ZKTConnect();
                    if (!res.IsSuccess)
                    {
                        return res;
                    }
                }
                int idwErrorCode = 0;
                axCZKEM1.EnableDevice(iMachineNumber, false);
                if (axCZKEM1.EnableUser(iMachineNumber, userId, 0, 0, enable))
                {
                    ZKTRefreshData();
                    //MessageBox.Show("The device will restart!", "Success");
                    axCZKEM1.EnableDevice(iMachineNumber, true);
                    return En_Result.CreateSuccessResult();
                }
                else
                {
                    axCZKEM1.GetLastError(ref idwErrorCode);
                    axCZKEM1.EnableDevice(iMachineNumber, true);
                    //MessageBox.Show("Operation failed,ErrorCode=" + idwErrorCode.ToString(), "Error");
                    return En_Result.CreateFailedResult("操作失败,错误代码:" + idwErrorCode.ToString());
                }
            }
            catch (Exception ex)
            {
                return En_Result.CreateFailedResult(ex.Message);
            }
        }

        public static En_Result ZKTDeleteEnrollData(int userId)
        {
            try
            {
                if (Session.Finger_IsConneted == false)
                {
                    var res = ZKTConnect();
                    if (!res.IsSuccess)
                    {
                        return res;
                    }
                }
                int idwErrorCode = 0;
                axCZKEM1.EnableDevice(iMachineNumber, false);
                //dwBackupNumber
                //指纹索引
                //一般范围为 0-9，同时会查询该用户是否还有其他指纹和密码，如都没有，则删除该用户
                //当为 10 是代表删除的是密码，同时会查询该用户是否有指纹数据，如没有，则删除该用户
                //11 是代表删除该用户所有指纹数据，12 代表删除该用户（包括所有指纹和卡号、密码数据）
                var isOk = axCZKEM1.DeleteEnrollData(iMachineNumber, userId, 1, 12);
                if (!isOk)
                {
                    axCZKEM1.GetLastError(ref idwErrorCode);
                    axCZKEM1.EnableDevice(iMachineNumber, true);
                    //MessageBox.Show("Operation failed,ErrorCode=" + idwErrorCode.ToString(), "Error");
                    return En_Result.CreateFailedResult("操作失败,错误代码:" + idwErrorCode.ToString());

                }
                else
                {
                    ZKTRefreshData();
                    axCZKEM1.EnableDevice(iMachineNumber, true);
                    //MessageBox.Show("The device will restart!", "Success");
                    return En_Result.CreateSuccessResult();
                }
            }
            catch (Exception ex)
            {
                return En_Result.CreateFailedResult(ex.Message);
            }
        }
        /// <summary>
        /// EnrollNumber为该用户的UserID IsInValid 为该记录是否为有效记录，1为无效记录，0为有效记录。 AttState为考勤状态 默认0—Check-In 4—OT-In 1—Check-Out2—Break-Out3—Break-In 5—OT-Out VerifyMethod为验证方式 一般情况下：0为密码验证，1为指纹验证，2为卡验证
        /// Year/Month/Day/Hour/Minute/Second为验证通过时的时间
        /// </summary>
        /// <param name="action">int EnrollNumber, int IsInValid, int AttState, int VerifyMethod, int Year, int Month, int Day, int Hour, int Minute, int Second</param>
        /// <returns></returns>
        public static En_Result RegEvntOnAttTransaction(Action<int, int, int, int, int, int, int, int, int, int> action)
        {
            try
            {
                if (Session.Finger_IsConneted == false)
                {
                    var res = ZKTConnect();
                    if (!res.IsSuccess)
                        return res;
                }
                int idwErrorCode = 0;
                if (axCZKEM1.RegEvent(iMachineNumber, 65535))
                {
                    ZKTRefreshData();
                    axCZKEM1.OnAttTransaction += new zkemkeeper._IZKEMEvents_OnAttTransactionEventHandler(action);
                    //MessageBox.Show("The device will restart!", "Success");
                    return En_Result.CreateSuccessResult();
                }
                else
                {
                    axCZKEM1.GetLastError(ref idwErrorCode);
                    //MessageBox.Show("Operation failed,ErrorCode=" + idwErrorCode.ToString(), "Error");
                    return En_Result.CreateFailedResult("操作失败,错误代码:" + idwErrorCode.ToString());
                }
            }
            catch (Exception ex)
            {
                return En_Result.CreateFailedResult(ex.Message);
            }
        }
        //
        /// <summary>
        /// 
        /// </summary>
        /// <param name="action">string EnrollNumber, int IsInValid, int AttState, int VerifyMethod, int Year, int Month, int Day, int Hour, int Minute, int Second, int WorkCode</param>
        /// <returns></returns>
        public static En_Result RegEvntOnAttTransactionEx(Action<string, int, int, int, int, int, int, int, int, int, int> action)
        {
            try
            {
                if (Session.Finger_IsConneted == false)
                {
                    var res = ZKTConnect();
                    if (!res.IsSuccess)
                        return res;
                }
                int idwErrorCode = 0;
                if (axCZKEM1.RegEvent(iMachineNumber, 65535))
                {
                    ZKTRefreshData();
                    axCZKEM1.OnAttTransactionEx += new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(action);
                    //MessageBox.Show("The device will restart!", "Success");
                    return En_Result.CreateSuccessResult();
                }
                else
                {
                    axCZKEM1.GetLastError(ref idwErrorCode);
                    //MessageBox.Show("Operation failed,ErrorCode=" + idwErrorCode.ToString(), "Error");
                    return En_Result.CreateFailedResult("操作失败,错误代码:" + idwErrorCode.ToString());
                }
            }
            catch (Exception ex)
            {
                return En_Result.CreateFailedResult(ex.Message);
            }
        }
    }
}
